package com.hndfsj.minio.config;

import com.hndfsj.minio.service.MinioClientService;
import com.hndfsj.minio.service.MinioFileService;
import com.hndfsj.minio.service.MinioTempCleanService;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({MinioProperties.class})
public class MinioConfiguration {

    @Autowired
    private MinioProperties minioProperties;

    public MinioConfiguration() {
    }

    @Bean
    @ConditionalOnMissingBean({MinioFileService.class})
    MinioFileService minioFileService() {
        return new MinioFileService();
    }

    @Bean
    @ConditionalOnMissingBean({MinioTempCleanService.class})
    MinioTempCleanService minioTempCleanService() {
        return new MinioTempCleanService();
    }

    @Bean
    @ConditionalOnMissingBean({MinioClientService.class})
    MinioClientService minioClientService() {
        return new MinioClientService();
    }

    @Bean
    @ConditionalOnMissingBean({MinioClient.class})
    MinioClient minioClient() {
        MinioClient client = new MinioClientService().getClient(minioProperties.getEndpoint(), minioProperties.getAccessKey(), minioProperties.getSecretKey());
        return client;
    }
}
